package com.lhsy.djcloud.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.crypto.SecureUtil;
import com.google.common.collect.Lists;
import com.lhsy.djcloud.common.ReturnT;
import com.lhsy.djcloud.controller.vo.UserVo;
import com.lhsy.djcloud.entity.Department;
import com.lhsy.djcloud.entity.Employee;
import com.lhsy.djcloud.entity.User;
import com.lhsy.djcloud.entity.dto.UserDto;
import com.lhsy.djcloud.service.DepartmentService;
import com.lhsy.djcloud.service.EmployeeService;
import com.lhsy.djcloud.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangzw
 * @since 2020-09-24
 */
@RestController
@RequestMapping("/user")
@Api(value = "Server-API",tags = "用户")
public class UserController extends BaseController{
    @Autowired
    private UserService userService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private EmployeeService employeeService;

    @ApiOperation(value = "修改密码")
    @RequestMapping(value = "/pwd",method = RequestMethod.GET)
    public ReturnT updatePwd(HttpServletRequest request,Integer userId,String oldPwd,String newPwd){
        UserDto currUser = getCurrUser(request);
        if (!currUser.getUserId().equals(userId)){
            return ReturnT.FAIL;
        }
        User user = userService.getById(userId);
        //校验密码
        if (!user.getPassword().equals(SecureUtil.md5(oldPwd))){
            return ReturnT.FAIL(ReturnT.FAIL_CODE,"用户名密码不正确");
        }
        user.setPassword(SecureUtil.md5(newPwd));
        userService.updateById(user);
        return ReturnT.SUCCESS;
    }

    @ApiOperation(value = "获取除自己之外的用户信息层级展示")
    @RequestMapping(value = "/noselfList",method = RequestMethod.GET)
    public ReturnT noselfList(HttpServletRequest request){
        //按组织层级节后加载
        UserDto currUser = getCurrUser(request);
        List<Department> departmentList = departmentService.selectNoSelf(currUser.getUserId(),currUser.getTenantId());
        return ReturnT.SUCCESS(departmentList);
    }

    @ApiOperation(value = "编辑当前登陆用户基本信息")
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public ReturnT edit(HttpServletRequest request,@RequestBody UserVo userVo){
        UserDto currUser = getCurrUser(request);
        User user = userService.getById(currUser.getUserId());
        if (user == null){
            return ReturnT.FAIL(ReturnT.FAIL_CODE,"用户不存在");
        }
        //编辑用户
        BeanUtil.copyProperties(userVo,user, CopyOptions.create().setIgnoreNullValue(true));
        userService.updateById(user);
        return ReturnT.SUCCESS;
    }

    @ApiOperation(value = "查看当前登陆用户基本信息")
    @RequestMapping(value = "/view",method = RequestMethod.GET)
    public ReturnT view(HttpServletRequest request){
        UserDto currUser = getCurrUser(request);
        User user = userService.getById(currUser.getUserId());
        Department department = departmentService.getById(user.getDeptId());
        Department byId = departmentService.getById(department.getFid());
        if (Objects.nonNull(byId)){
            department.setFDeptName(byId.getDeptName());
        }
        user.setDepartment(department);
        user.setPassword(null);
        //查询党员人数
        //查询当前党下的所有党小组
        List<Integer> deptidsSearch = Lists.newArrayList();
        if (department.getLevel() == 1){
            //查询所有的党小组
            List<Department> departments = departmentService.getByLevel(currUser.getTenantId(), 3);
            deptidsSearch  = departments.stream().map(Department::getId).collect(Collectors.toList());
        }
        if (department.getLevel() == 2){
            List<Department> departments = departmentService.selectByPid(byId.getId());
            deptidsSearch  = departments.stream().map(Department::getId).collect(Collectors.toList());
        }
        if (department.getLevel() == 3){
            List<Department> departments = departmentService.selectByPid(byId.getId());
            deptidsSearch  = departments.stream().map(Department::getId).collect(Collectors.toList());
        }
        List<Employee> employees = employeeService.selectByDeptIds(deptidsSearch);
        user.setCount(employees.size());
        return ReturnT.SUCCESS(user);
    }
}

